﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CNCSoft.EData;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

using Common;

namespace CNCSoft.ErpApps.TMS.Academic
{
    public partial class frmInNguyenLop : Form
    {
        private string strMaLop = "";
        private string strReportFile = "";

        public frmInNguyenLop(string _strReportFile,  string _strMaLop)
        {
            InitializeComponent();

            strMaLop = _strMaLop;
            strReportFile = _strReportFile;
        }

        private void frmInNguyenLop_Load(object sender, EventArgs e)
        {
            this.Text = this.Text + "   " + strMaLop;

            rpt.Load(strReportFile);
            PublicFunction.SetDBLogonForReport(rpt);

            progressBar1.Maximum = 0;
            progressBar1.Maximum = Convert.ToInt16(SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString, "SELECT count(MaHocTap) FROM HOCSINH_LOP WHERE  MaLop = '" + strMaLop + "' " +
                                                                                                               "AND MaTrangThai IN ('SS', 'TD') group by MaLop ").ToString());
            lblDescription.Text = "Lớp này có " + progressBar1.Maximum.ToString("G") + " Học sinh";
        }

        
        ReportDocument rpt = new ReportDocument();

        private void InMotHocSinh(string pMaHocTap)
        {
            try
            {
                
                //PublicFunction.SetParameter(rpt, "pThoiGian", ReportUtilities.ReportUtilities.ThoiGianDep(txtTuNgay.getDateValue(), txtDenNgay.getDateValue()));
                //PublicFunction.SetParameter(rpt, "pMoRongDong", chkMoRongDong.Checked);

                PublicFunction.SetParameter(rpt, "@pMaHocTap", pMaHocTap);

                PublicFunction.SetParameter(rpt, "pGVCN", " ");
                PublicFunction.SetParameter(rpt, "pGVCN", SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                    "SELECT TenGiaoVien FROM DM_LOP join DM_GIAOVIEN on DM_LOP.MaGiaoVien = DM_GIAOVIEN.MaGiaoVien where MaLop = '" + strMaLop + "'"));

                //Gan report source
                rpt.PrintToPrinter(1, false, 1, 1);
                //CRViewer.ReportSource = rpt;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "cmdXem error!");
            }
        }

        private bool bCanCelPrint = false;

        private void cmdInLienTuc_Click(object sender, EventArgs e)
        {
            SqlDataReader HV_DataReader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                                                      "SELECT MaHocTap, HoLot + ' ' + Ten as HoTen FROM HOCSINH_LOP " + 
                                                      "JOIN HOCSINH ON HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh " +
                                                      "WHERE  HOCSINH_LOP.MaLop = '" + strMaLop + "'" +
                                                      "AND MaTrangThai IN ('SS', 'TD') " +
                                                      " ORDER BY STT_SoCai");
            
            while (HV_DataReader.Read() && !bCanCelPrint)
            {
                try
                {
                    progressBar1.Value++;
                    string HS_MaHocTap = HV_DataReader.GetValue(0).ToString().Trim();
                    string HS_HoTen = HV_DataReader.GetValue(1).ToString().Trim();
                    lblDescription.Text = "Đang in phiếu điểm của HS: " + HS_HoTen;
                    InMotHocSinh(HS_MaHocTap);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void cmdCanCel_Click(object sender, EventArgs e)
        {
            bCanCelPrint = true;
        }

    }
}
